// PIC18F14K50 vm18
// Laungage : MPLAB C18
#include <p18f14k50.h>
#include <delays.h>
#include "lib_adc.h"
// configration
#pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit
#pragma config USBDIV = OFF //USB Clock Selection bit
#pragma config FOSC = IRC //HS //Oscillator Selection bits
#pragma config PLLEN = ON //X PLL Enable bit
#pragma config PCLKEN = OFF //Primary Clock Enable Bit
#pragma config FCMEN = OFF
#pragma config IESO = OFF //Internal/External Oscillator Switchover bit
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF //MCLR Pin Enable bit
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768
#define lcd_DB LATB
#define lcd_E LATCbits.LATC3
#define lcd_RS LATCbits.LATC4
#define Delay_50uS Delay10TCYx(5)
#define Delay_1mS Delay1KTCYx(1)
#define Delay_2mS Delay1KTCYx(2)
#define Delay_5mS Delay1KTCYx(5)
#define Delay_20mS Delay1KTCYx(20)
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1; // Enable Hi
Delay1TCY(); // NOP
lcd_E = 0; // Enable Low
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putv(int v){
char i;
char buf[5];
v *=49;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);
lcd_putchr(buf[0]);
lcd_putchr('.');
for(i=1;i<4;i++)
lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
char unit[] = "(V)";
char ch1[] = "ch1 ";
char ch2[] = "ch2 ";
void main(void){
int v,v0,v1;
v0 = 1;
v1 = 1;
OSCCON = 0b01010010; // 内部クロック4Mhz
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b11000000;
ANSEL = 0b00000000;
ANSELH = 0b00000011;
lcd_init();
while(1){
Delay_20mS;
OpenADC(ADC_FOSC_4 & // ADC初期化
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH8 &
ADC_INT_OFF );
ConvertADC(); // AD変換開始
while(BusyADC()); // AD変換終了待ち
v=(ADRESH*256) + ADRESL; // AD変換結果取得
if(v!=v0){
v0=v;
lcd_locate(0,2);
lcd_puts(ch1);
lcd_putv(v0);
lcd_puts(unit);
}
OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );
ConvertADC();
while(BusyADC());
v=(ADRESH*256) + ADRESL;
if(v!=v1){
v1=v;
lcd_locate(1,2);
lcd_puts(ch2);
lcd_putv(v1);
lcd_puts(unit);
}
}
}